<template>
  <el-card>
    <div slot="header" class="clearfix">
      <span>订单审核</span>
    </div>
    <div class="home-container">
      <div class="clearfix">
        <v-searchForm
          :form-data="filter_info"
          :form-title="formTitle"
          @search="search"
          @reset="reset"
        />
      </div>
      <v-tableBtns
        :show-export="false"
        title="订单审核"
      />
      <v-table
        ref="table"
        :table-title="tableTitle"
        :table-data="tableData"
        :deletable="false"
        :header-cell-style="changeStyle"
        @sortChange="sortChange"
        @del="del"
        @edit="edit"
        edit-title="审批"
      />
      <v-pagination
        :page-info="pageInfo"
        :get-table-list="getTableList"
      />

    </div>
  </el-card>
</template>

<script>
  import { mapGetters } from 'vuex'
  import UploadExcelComponent from '@/components/UploadExcel/index.vue'
  import { parseTime } from '../../../utils'
  import auth from '@/mixins/auth'
  import { $get } from '@/utils/newRequest'

  export default {
    name: 'Home',
    mixins: [auth],
    components: { UploadExcelComponent },
    data() {
      const now = new Date()
      now.setTime(now.getTime() - 3600 * 1000 * 24 * (now.getDate() - 1))
      const startTime = parseTime(now, '{y}-{m}-{d}')
      const endTime = parseTime(new Date(now.getFullYear(), now.getMonth() + 1, 0), '{y}-{m}-{d}')

      return {
        uploadUrl: process.env.BASE_API + 'action/WechatAdvs/saveFile',
        index: [false, false, false],
        formTitle: [
          {
            cn: '订单id：',
            en: 'ad_id'
          },
          {
            cn: '实际广告主：',
            en: 'ad_name'
          },
          {
            cn: '公众号：',
            en: 'name'
          },
          {
            cn: '商务：',
            en: 'swname',
            placeholder: '输入全称'
          },
          {
            cn: '收款状态：',
            en: 'is_receive',
            type: 'select',
            list: [
              {
                label: '未收款',
                value: 0
              },
              {
                label: '已收款',
                value: 2
              }
            ]
          },
          {
            cn: '合同编号：',
            en: 'contract_num',
            type: 'select',
            list: [
              {
                label: '有',
                value: 1
              },
              {
                label: '无',
                value: 0
              }
            ]
          },
          {
            cn: '是否开票：',
            en: 'is_bill',
            type: 'select',
            list: [
              {
                label: '是',
                value: 1
              },
              {
                label: '否',
                value: 0
              }
            ]
          },
          {
            cn: '发布时间：',
            en: 'created_at',
            type: 'time'
          }
        ],
        filter_info: {
          // created_at: [startTime, endTime]
        },
        originFilter: {
          // created_at: [startTime, endTime]
        },
        tableTitle: [
          {
            cn: 'id',
            en: 'ad_id',
            width: '80px',
            // sortable: true
          },
          {
            cn: '公众号',
            en: 'name',
            // sortable: true
          },
          {
            cn: '改后发布时间',
            en: 'update_publish_at',
            // sortable: true
          },
          {
            cn: '改后金额',
            en: 'update_ad_price',
            // sortable: true
          },
          {
            cn: '版位',
            en: 'ad_place_name'
          },
          {
            cn: '实际广告主',
            en: 'ad_name'
          },
          {
            cn: '品牌名',
            en: 'brand'
          },
          {
            cn: '微赞收款',
            en: 'real_price',
            // sortable: true
          },
          {
            cn: '微萊收款',
            en: 'wl_real_price',
            // sortable: true
          },
          {
            cn: '商务',
            en: 'swname',
            // sortable: true
          },
          // {
          //   cn: '提交人',
          //   en: 'fans_name'
          // },
          {
            cn: '修改方式',
            en: 'approval_delect',
            formatter(record){
              switch(record.approval_delect){
                case 0: return '修改订单'
                case 1: return '删除订单'
                default: return '修改订单'
              }
            }
          },
          {
            cn: '审批状态',
            en: 'approval_state',
            formatter(record){
              switch(record.approval_state){
                case 0: return '待审核'
                case 1: return '未通过'
                case 2: return '审核通过'
                default: return ''
              }
            }
          }
        ],
        tableData: [],

        // 排序
        sidx: 'publish_at',
        sortKey: null,

        // f分页
        pageInfo: {
          pageNumber: 1,
          pageSize: 30,
          totalPage: 0,
          totalRow: 0
        }
      }
    },
    computed: {
      ...mapGetters([
        'name',
        'roles'
      ]),
      filter_infos() {
        const data = {}
        const origin = this.filter_info
        for (const key in origin) {
          if (!this.filter_info[key] && this.filter_info[key] !== 0) {
            this.$delete(this.filter_info, key)
            continue
          }
          if (key === 'created_at') {
            data.start_time = origin[key][0]
            data.End_time = origin[key][1]
          } else {
            data[key] = origin[key]
          }
        }
        if (!origin.created_at) {
          delete data.start_time
          delete data.End_time
        }
        return data
      }
    },
    activated() {
      this.getTableList()
    },
    methods: {
      changeStyle({ row, column, rowIndex, columnIndex }) {
        console.log(row, column, rowIndex, columnIndex)
      },
      getTableList() {
        const loading = this.$loading()

        const datas = {
          ...this.filter_infos,
          page: this.pageInfo.pageNumber, rows: this.pageInfo.pageSize,
          // sidx: this.sidx,
          // sortKey: this.sortKey,
          type:0
        }
        this.$get('action/UpdateWechatAdvs/dataGrid', datas)
          .then(res => {
            let list
            const data = res.data
            if (data.list && data.list.length) {
              list = data.list.map((item, index) => {
                switch (item.invoice_state) {
                  case 0:
                    item.invoice_state = '未开票'
                    break
                  case 1:
                    item.invoice_state = '开票中'
                    break
                  case 2:
                    item.invoice_state = '已开票'
                    break
                }
                return item
              })
            }
            this.pageInfo.totalPage = data.totalPage
            this.pageInfo.totalRow = data.totalRow

            this.tableData = list || []
            loading.close()
          })
          .catch(err => {
            loading.close()
            this.$message({
              message: '获取数据失败',
              type: 'error'
            })
          })
      },
      // 搜索
      search() {
        this.pageInfo.pageNumber = 1
        this.getTableList()
      },
      reset() {
        this.filter_info = {}
        this.index = []
        this.getTableList()
      },

      del(val) {
        const data = { ad_id: val.row.ad_id }
        this.$post('action/WechatAdvs/del', data)
          .then(res => {
            this.getTableList()
            this.$message({
              message: res.msg,
              type: 'success'
            })
          })
          .catch(_ => {

          })
      },
      upload() {
        // this.$router.push({
        //   name: 'adv_order.upload'
        // })
      },

      edit(val) {
        const details = JSON.stringify(val.row)
        this.$router.push({
          name: 'adv_order.check_info',
          query: { details}
        })
      },
      addnew() {
        console.log(1)
      },
      // 排序
      sortChange({ column, prop, order }) {
        this.sidx = prop || 'publish_at'
        this.sortKey = order === 'ascending' ? 'asc' : null
        this.getTableList()
      }

    }

  }
</script>

<style rel="stylesheet/scss" lang="scss">

</style>
